﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ArithCollect;
using System.Linq;

namespace UnitTestProject1 {
    [TestClass]
    public class SeriesTest1 {
        [TestMethod]
        public void m15() {
            var x = new[] { 1, 3, 5, 7, 9, 11, 13, 15 };
            var y = new[] { 0, 2, 4, 6, 8, 20 };
            var r=x.SortMerge(y);
            foreach (var item in r) {
                Console.Write("{0},", item);
            }
        }
        [TestMethod]
        public void m14() {
            var x = new[] { 1, 3, 5, 7, 9, 11, 13, 15 };
            var y = new[] { 0, 2, 4, 6, 8, 20 };
            var xlen = x.Length; var ylen = y.Length;
            var rlen = xlen + ylen - 1;
            var r = new int[rlen + 1];
            int xidx, yidx; xidx = yidx = 0; var idx = 0;
            while (xidx < xlen && yidx < ylen) {
                if (x[xidx] > y[yidx]) r[idx++] = y[yidx++];
                else r[idx++] = x[xidx++];
            }
            if (xidx >= xlen) for (int i = yidx; i < ylen; i++) r[idx++] = y[i];
            else for (int i = xidx; i < xlen; i++) r[idx++] = x[i];
            foreach (var item in r) {
                Console.Write("{0},", item);
            }
        }
        [TestMethod]
        public void m13() {
            var x = new[] { 1, 3, 5, 7, 9 };
            var y = new[] { 0, 2, 4, 6, 8 };
            var xlen = x.Length; var ylen = y.Length;
            var rlen = xlen + ylen - 1;
            var r = new int[rlen + 1];
            int xidx, yidx; xidx = yidx = 0; var idx = 0;
            while (xidx < xlen && yidx < ylen) {
                if (x[xidx] > y[yidx]) {
                    r[idx++] = y[yidx++];
                } else {
                    r[idx++] = x[xidx++];
                }
            }
            if (xidx >= xlen) {
                y.CutOut(yidx, ylen - 1).ToArray().CopyTo(r, idx);
            } else {
                x.CutOut(xidx, xlen - 1).ToArray().CopyTo(r, idx);
            }
            foreach (var item in r) {
                Console.Write("{0},", item);
            }
        }
        [TestMethod]
        public void m12() {
            var x = new[] { 1, 3, 5, 7, 9 };
            var y = new[] { 0, 2, 4, 6, 8 };
            var xlen = x.Length; var ylen = y.Length;
            var rlen = xlen + ylen - 1;
            var r = new int[rlen + 1];
            int left, right; left = right = 0;
            for (int i = 0; i < rlen; i++) {
                r[i] = x[left] > y[right] ? y[right++] : x[left++];
                Console.Write("{0},", r[i]);
            }
            var last = r[rlen] = x[xlen - 1] < y[ylen - 1] ? y[ylen - 1] : x[xlen - 1];
            Console.Write("{0}", last);

        }
        [TestMethod]
        //归并2个有序集合 成为1个
        public void m11() {
            var x = new[] { 1, 3, 5, 7, 9 };
            var y = new[] { 0, 2, 4, 6, 8 };
            var r = new int[x.Length + y.Length];
            int left, right; left = right = 0;
            for (int i = 0; i < x.Length + y.Length - 1; i++) {
                r[i] = x[left] > y[right] ? y[right++] : x[left++];
                Console.Write("{0},", r[i]);
            }
            var last = r[x.Length + y.Length - 1] = x[x.Length - 1] < y[y.Length - 1] ? y[y.Length - 1] : x[x.Length - 1];
            Console.Write("{0}", last);
        }
        [TestMethod]
        public void AccessByXyzFrom3DArray() {
            var ss = new[, ,]{
                {{35,2,3,8,72},{215,21,3,8800,755},{35,1,34,18,61}},
                {{215,1,35,188,75},{125,11,333,88,127},{225,103,3,42,35}},
                {{77,41,13,808,715},{525,51,23,88,753},{20,10,3,85,75}},
            };
            var x = ss.This(1);
            foreach (var item in x) {
                Console.Write("{0},", item);
            }
            Console.WriteLine();
            var x2 = ss.This(1, true);
            foreach (var item in x2) {
                Console.Write("{0},", item);
            }
            Console.WriteLine();
            var x3 = ss.This(1, false);
            foreach (var item in x3) {
                Console.Write("{0},", item);
            }
        }
        [TestMethod]
        public void AccessByRowOrCol2DArray() {
            var ss = new string[,]{
                {"hellow","world","you","and"},
                {"this","is","ftanstic","absonule"},
                {"hemm","fukk","tosss","nba"}};
            Console.WriteLine(ss[2, 1]);
            Console.WriteLine("---");
            var ss2 = ss.This(2, false);
            foreach (var item in ss2) {
                Console.WriteLine(item);
            }
        }
    }
}